package zdream.control.test.foe.mmcommon;

import org.junit.Test;

import zdream.control.foe.mm.MegaMan;
import zdream.control.foe.mmcommon.HPCapsuleA;
import zdream.control.level.LevelManagerForTest;
import zdream.control.profile.mm.MegaManProfile;
import zdream.control.region.Region;
import zdream.control.region.SimpleRegionBuilder;
import zdream.control.world.LevelWorld;
import zdream.control.world.MFoe;

import static org.junit.Assert.*;

public class HPCapsuleCase {

	@Test
	public void testCreateLevelManager() {
		Region region = new SimpleRegionBuilder(this.getClass().getSimpleName())
				.withRoomPositions("1")
				.withRoom(1)
						.terrain("#########################")
						.terrain("#########################")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##         S           ##")
						.terrain("#########################")
						.terrain("#########################")
						.terrain("#########################")
						.build()
				.withStartPoint('S')
				.build();
		System.out.println(region);
		assertEquals(1, region.rooms.length);

		MegaManProfile p1 = new MegaManProfile();

		LevelManagerForTest manager = LevelManagerForTest.builder()
				.withPlayer1(p1)
				.build();
		assertEquals(LevelWorld.STATE_NONE, manager.state);
		manager.applyWorld(region);
		manager.runUntil(ignored -> manager.state == LevelWorld.STATE_PLAYING);
		assertNotNull(manager.player1);

		MegaMan player1 = (MegaMan) manager.player1;
		player1.healthModule.hp /= 2;
		int stage1HP = player1.healthModule.hp;
		System.out.printf("%s 的当前血量是 %d/%d%n", player1, player1.healthModule.hp, player1.healthModule.hpMax);

		System.out.println("在 player1 的正上方丢一个回血胶囊, 看看回不回血");
		MFoe capsule = manager.createFoe(
				HPCapsuleA.NAME, player1.anchorX, player1.anchorY + 5, true);
		float startY = capsule.anchorY;
		manager.runOneFrame();
		assertTrue(capsule.anchorY < startY);
		manager.everyTick(ignored ->
				System.out.printf("胶囊是否被删除: %s, 当前高度: %f%n", capsule.isDestroyed(), capsule.anchorY));

		manager.runUntil(ignored -> capsule.isDestroyed());
		assertTrue(capsule.pos.bottom() < player1.pos.top());
		System.out.printf("%s 的当前血量是 %d/%d%n", player1, player1.healthModule.hp, player1.healthModule.hpMax);
		assertTrue(player1.healthModule.hp > stage1HP);
	}
}
